package cheesymock;

import java.lang.reflect.*;

public class StrictPropertyOverride extends Validator {
	@Override
	public void creation(Class<?> clazz, Object delegate) {
		if (delegate != null) {
			for(Field f: delegate.getClass().getDeclaredFields()) {
				if (!f.getName().startsWith("this$") && !f.getName().startsWith("val$") && 
						f.getAnnotation(Ignore.class) == null) {
					Type r = f.getType();
					String n = f.getName().substring(0, 1).toUpperCase() 
						+ f.getName().substring(1);
					Class<?>[] t = new Class<?>[0];
					try {
						if(r.equals(clazz.getMethod("get" + n, t).getReturnType())) break;
					}
					catch (NoSuchMethodException e) {
					}
					try {
						if(r.equals(clazz.getMethod("is" + n, t).getReturnType())) break;
					}
					catch (NoSuchMethodException e) {
					}
					try {
						if(Void.TYPE.equals(clazz.getMethod("set" + n, new Class<?>[] { f.getType() }).getReturnType())) break;
					}
					catch (NoSuchMethodException e) {
					}
					throw new IllegalArgumentException("Delegate object has declared field " + 
							f.getName() + ", but a corresponding getter/setter was not found " +
							"in interface " + clazz.getName() + ". A refactoring gone wrong, perhaps?");
				}
			}
		}
	}
}